package cloud.ben.smallscalepigfarmmanagementsystem.service.impl.pig;

import cloud.ben.smallscalepigfarmmanagementsystem.entity.drug.Drug;
import cloud.ben.smallscalepigfarmmanagementsystem.entity.pig.Pig;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.drug.DrugMapper;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.pig.PigMapper;
import cloud.ben.smallscalepigfarmmanagementsystem.service.impl.drug.DrugServiceImpl;
import cloud.ben.smallscalepigfarmmanagementsystem.service.pig.IPigService;
import cloud.ben.smallscalepigfarmmanagementsystem.utils.RespBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Ben
 * @Date: 2022/4/6 11:02
 */
@Service
public class PigServiceImpl implements IPigService {


    protected static final Logger logger = LoggerFactory.getLogger(PigServiceImpl.class);

    @Autowired
    private PigMapper pigMapper;
    @Override
    public RespBean insertPig(Pig pig) {
        QueryWrapper<Pig> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("pig_name", pig.getPigName());
        List<Pig> list = pigMapper.selectList(queryWrapper);
        if (list.size() == 0) {
            if (pigMapper.insert(pig) == 1){
                logger.info("插入成功");
                return RespBean.ok("插入成功");
            }
            logger.error("系统异常，插入失败");
            return  RespBean.error("系统异常，插入失败");
        }
        logger.info("您输入的数据已经存在，插入失败");
        return  RespBean.error("您输入的数据已经存在，插入失败");
    }

    @Override
    public RespBean deletePig(Integer id) {
        QueryWrapper<Pig> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("id", id);
        List<Pig> list = pigMapper.selectList(queryWrapper);
        if (list != null) {
            if (pigMapper.delete(queryWrapper)!= 0){
                logger.info("删除成功");
                return  RespBean.ok("删除成功");
            }
            logger.error("系统异常，删除失败");
            return RespBean.error("系统异常，删除失败");
        }
        logger.info("您要删除的数据不存在，删除失败");
        return RespBean.error("您要删除的数据不存在，删除失败");
    }

    @Override
    public RespBean updatePig(Pig pig) {
        QueryWrapper<Pig> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("id", pig.getId());
        List<Pig> list = pigMapper.selectList(queryWrapper);
        if (list != null) {
            if (pigMapper.update(pig, queryWrapper)!= 0){
                logger.info("更新成功");
                return RespBean.ok("更新成功");
            }
            logger.error("系统异常，更新失败");
        }
        logger.error("您要更新的数据不存在，更新失败");
        return RespBean.error("您要更新的数据不存在，更新失败");
    }

    @Override
    public RespBean selectPigByPage(Integer pageNumber, Integer pageSize) {
        Page<Pig> page = new Page<>(pageNumber, pageSize);
        IPage<Pig> pigIPage=pigMapper.selectPage(page, null);
        return RespBean.ok("查询成功",pigIPage);
    }

    @Override
    public RespBean selectPigNum() {
        return RespBean.ok("查询成功",  pigMapper.selectCount(null));
    }
}
